package com.cby.springboot.demo.mapper;

import com.cby.springboot.demo.bean.Book;
import com.cby.springboot.demo.bean.Category;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface BookMapper {

    @Insert("insert ingo category ( id, name ) values ( #{id,jdbcType=INTEGER}, #{name,jdbcType=VARCHAR} )")
    int addCategory(Category category);

    @Update("update book " +
            "    set cover = #{userId,jdbcType=VARCHAR},\n" +
            "      title = #{userName,jdbcType=VARCHAR},\n" +
            "      author = #{password,jdbcType=VARCHAR},\n" +
            "      date = #{userName,jdbcType=VARCHAR},\n" +
            "      press = #{password,jdbcType=VARCHAR},\n" +
            "      abs = #{email,jdbcType=VARCHAR}\n" +
            "    where id = #{id,jdbcType=INTEGER}")
    int updateBook(Book book);

    @Update("update book " +
            "    set cid = #{cid,jdbcType=INTEGER}\n" +
            "    where title = #{title,jdbcType=VARCHAR}")
    int updateBookForCId(String title, int cid);

    @Insert("insert into book ( cover, title, author, date, press, abs)" +
            "values ( #{cover,jdbcType=VARCHAR}, #{title,jdbcType=VARCHAR}, #{author,jdbcType=VARCHAR}, #{date,jdbcType=VARCHAR}, #{press,jdbcType=VARCHAR}, #{abs,jdbcType=VARCHAR})")
    int addBook(Book book);

    @Delete("delete from book where id=#{id}")
    int deleteById(@Param("id") int id);

    @Select("select * from book order by id desc")
    List<Book> list();

    @Select("select * from book where cid = #{cid}")
    List<Book> findAllByCategory(@Param("cid") int cid);


//    @Select("select * from book where title LIKE CONCAT('%',#{title,jdbcType=VARCHAR},'%')")
    @Select("<script>"+
            "select * " +
            " from book " +
            " where " +
            " title like concat('%',#{title},'%')"+
            " or author like concat('%',#{author},'%')"+
            "</script>")
    List<Book> findAllByTitleLikeOrAuthorLike(String title, String author);
}
